import {defineStore} from "pinia";

let useCounterStore = defineStore(
    "counter",
    {
        state: () => {
            return {
                count: 0,
            }
        },

        // 用来获取对象中的数据，可以在获取的时候进行处理
        getters: {
            oddOrEven() {
                return this.count % 2 === 1 ? "奇数" : "偶数"
            }
        },
        // 修改对象中的数据
        actions: {
            increment(number) {
                this.count = this.count + number
            },
            decrement(number) {
                this.count = this.count - number
            },
            incrementIfOdd(number) {
                if (this.count % 2 === 1) {
                    this.increment(number)
                }
            },
            incrementAsync(number) {
                setTimeout(
                    () => {
                        this.increment(number)
                    },
                    1000
                )
            }
        },

        persist: true,  // 启用持久化
    }
);

export default useCounterStore